package com.agriculture.controller;

import com.agriculture.common.R;
import com.agriculture.dto.CommentDto;
import com.agriculture.entity.*;
import com.agriculture.service.CommentService;
import com.agriculture.service.UserService;
import com.agriculture.service.GoodService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;
    @Autowired
    private GoodService goodService;
    @PostMapping
    public R<String> addComment(@RequestBody Comment comment){
        // 要求前端传来的数据里含有 goodId
        commentService.save(comment);
        return R.success("新增评论成功！");
    }

    /**
     * 查询某个商品的所有评论
     * @return
     */
    @GetMapping("/list/{id}")
    public R<List<CommentDto>> getAll(@PathVariable Long id){
        return R.success(commentService.findByGoodId(id));
    }

    /**
     * 查询所有商品评论
     * @return
     */
    @GetMapping("/getall")
    public R<List<CommentDto>> getAllCommit(){
        return R.success(commentService.getAll());
    }
    /**
     * 删除商品评论
     * @return
     */
    @DeleteMapping
    public R<String> delCarousel(@RequestBody Comment comment){
        commentService.removeById(comment);
        return R.success("评论删除成功!");
    }

    /**
     * 评论分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //构造分页构造器对象
        Page<Comment> pageInfo = new Page<>(page,pageSize);
        Page<CommentDto> CommentDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
//        queryWrapper.like(name != null, Comment::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Comment::getCreateTime);
//        queryWrapper.eq(Good::getIsDeleted,0);
        //执行分页查询
        commentService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,CommentDtoPage,"records");

        List<Comment> records = pageInfo.getRecords();

        List<CommentDto> list = records.stream().map((item) -> {
            CommentDto commentDto = new CommentDto();

            BeanUtils.copyProperties(item,commentDto);
            Long userId = item.getUserId();
            Long goodId = item.getGoodId();

            //根据id查询对象
            User user = userService.getById(userId);
            Good good = goodService.getById(goodId);

            if(user != null){
                String userName = user.getName();
                commentDto.setUserName(userName);
            }
            if(good != null){
                String goodName = good.getName();
                commentDto.setGoodName(goodName);
            }

            return commentDto;
        }).collect(Collectors.toList());

        CommentDtoPage.setRecords(list);

        return R.success(CommentDtoPage);
    }


}









